package com.ayf.payment.game.api.utils;

import com.ayf.payment.game.api.dto.TemplateChannelDTO;
import com.ayf.payment.game.api.dto.TemplateEquipmentDTO;
import com.ayf.payment.game.api.dto.TemplateEscitationDTO;
import com.ayf.payment.game.api.dto.criteria.TemplateCriteria;
import com.ayf.payment.game.api.dto.getaway.*;
import com.ayf.payment.game.api.type.AdditionalStatus;
import com.ayf.payment.game.api.type.YesAndNo;
import org.springframework.util.CollectionUtils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 按充值条件 获取赠送充值金额
 *
 * @author wwl
 * @date 2019/12/26 19:29
 */
public class TemplateFormatUtil {

    //计算总充值金额
    public static void getTotalAmount(TemplateCriteria template, String payCode, GameRechargeGetWayDTO rechargeGetWayDTO) {
        int amount = rechargeGetWayDTO.getAmount();
        int totalAmount=amount*10;
        int giveAmount=0;
        if (!CollectionUtils.isEmpty(template.getEscitationList())) {
            int escitation = getEscitationMap_1(template.getEscitationList(), amount,rechargeGetWayDTO);
            rechargeGetWayDTO.setEscitationOfMoney(escitation);
            giveAmount=giveAmount+escitation*10;

        }
        if (template.getIsRepaidGift() == YesAndNo.YES) {
            Map<String, TemplateChannelDTO> channelMap = getChannelMap_1(template.getChannelList());
            TemplateChannelDTO channelDTO=channelMap.get(payCode);
            Integer donationRatio=channelDTO.getDonationRatio();
            rechargeGetWayDTO.setChannelDTO(channelDTO);
            if(null == donationRatio){
                donationRatio=0;
            }
            int channelAmount=totalAmount;
            if (template.getIsShowAmount() == YesAndNo.YES) {
                channelAmount=channelAmount+rechargeGetWayDTO.getEscitationOfMoney()*10;
            }
            int channelOfMoney = channelAmount * donationRatio / 100;
            rechargeGetWayDTO.setChannelOfMoney(channelOfMoney);
            giveAmount=giveAmount+channelOfMoney;
            // totalAmount = totalAmount + channelOfMoney;

        }
        totalAmount=totalAmount+rechargeGetWayDTO.getChannelOfMoney()+rechargeGetWayDTO.getEscitationOfMoney()*10;
        rechargeGetWayDTO.setGiveMoney(formatMoney(giveAmount));
        rechargeGetWayDTO.setAmountOfMoney(totalAmount);
    }

    public static void formatTemplateReissueAll(TemplateCriteria template, int rechargeAmount, int amount, String payCode, GameRechargeGetWayDTO rechargeGetWayDTO, Integer reissueQuota, boolean typeTwo) {

        // 附加赠送
        if (!CollectionUtils.isEmpty(template.getAdditionalList())) {
            List<TemplateAdditionalGetaWayDTO> additionalList = new ArrayList<>();
            template.getAdditionalList().forEach((additional) -> {
                if (additional.getAdditionalStatus().ordinal() > AdditionalStatus.CLOSE.ordinal()) {
                    TemplateAdditionalGetaWayDTO tag = new TemplateAdditionalGetaWayDTO();
                    tag.setAdditionalStatus(additional.getAdditionalStatus());
                    tag.setDonationRatio(additional.getDonationRatio());
                    tag.setIsShow(additional.getIsShow());
                    tag.setMaterialName(additional.getMaterialName());
                    Map map=templateOptionSUM_1(rechargeAmount,amount, payCode, additional.getAdditionalStatus(),reissueQuota, template,typeTwo);
                    tag.setTotalCount((Integer) map.get("countAmount"));
                    tag.setGiveMoney((String) map.get("giveMoney"));
                    tag.setGiveTotal(String.valueOf(tag.getTotalCount()*tag.getDonationRatio()/10));
                    additionalList.add(tag);
                }
            });
            rechargeGetWayDTO.setAdditionalList(additionalList);

        }
        // 积分
        if (!CollectionUtils.isEmpty(template.getIntegralList())) {
            List<TemplateIntegralGetaWayDTO> integralList = new ArrayList<>();
            template.getIntegralList().forEach((integral) -> {
                if (integral.getIntegralStatus().ordinal() > AdditionalStatus.CLOSE.ordinal()) {
                    TemplateIntegralGetaWayDTO tig = new TemplateIntegralGetaWayDTO();
                    tig.setDonationRatio(integral.getDonationRatio());

                    tig.setIntegralName(integral.getIntegralName());
                    tig.setIntegralStatus(integral.getIntegralStatus());
                    Map map=templateOptionSUM_1(rechargeAmount,amount, payCode, integral.getIntegralStatus(),reissueQuota, template,typeTwo);
                    tig.setTotalCount((Integer) map.get("countAmount"));
                    tig.setGiveMoney((String) map.get("giveMoney"));
                    tig.setGiveTotal(String.valueOf(tig.getTotalCount()*tig.getDonationRatio()/10));
                   // tig.setTotalCount(templateOptionSUM_1(rechargeAmount,amount, payCode, integral.getIntegralStatus(), reissueQuota, template,typeTwo));
                    integralList.add(tig);
                }
            });
            rechargeGetWayDTO.setIntegralList(integralList);
        }
        // 自定义
        if (template.getCustomizeStatus()!=null && template.getCustomizeStatus()!= AdditionalStatus.CLOSE && !CollectionUtils.isEmpty(template.getCustomizeList())) {
            TemplateCustomizeGetaWayDTO customize = new TemplateCustomizeGetaWayDTO();
            customize.setCustomizeStatus(template.getCustomizeStatus());
            Map map=templateOptionSUM_1(rechargeAmount,amount, payCode, template.getCustomizeStatus(),reissueQuota, template,typeTwo);
            customize.setTotalCount((Integer) map.get("countAmount"));
            customize.setGiveMoney((String) map.get("giveMoney"));
            rechargeGetWayDTO.setCustomizeStatus(template.getCustomizeStatus());
            rechargeGetWayDTO.setCustomizeGetaWay(customize);
        }
        // 装备
        if (template.getEquipmentStatus().ordinal() > AdditionalStatus.CLOSE.ordinal()) {
            List<TemplateEquipmentGetaWayDTO> equipmentList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(template.getEquipmentList())) {
                int totalCount = rechargeAmount/10;
                if(template.getShowEquipmentType()==null ||template.getShowEquipmentType()==1) {
                    TemplateEquipmentGetaWayDTO teg = new TemplateEquipmentGetaWayDTO();
                    teg.setEquipmentStatus(AdditionalStatus.CLOSE);
                    int num = 0;
                    for (TemplateEquipmentDTO te : template.getEquipmentList()) {
                        int rechargeAmount1 = te.getRechargeAmount().intValue();
                        if (totalCount >= rechargeAmount1 && num < rechargeAmount1) {
                            num = rechargeAmount1;
                            teg.setGiveUP(true);
                            teg.setEquipmentNameNumber(te.getEquipmentNameNumber());
                            teg.setRechargeAmount(rechargeAmount1);
                            teg.setEquipmentStatus(template.getEquipmentStatus());
                        }
                    }
                    equipmentList.add(teg);
                }else {

                        for (TemplateEquipmentDTO te : template.getEquipmentList()) {
                            TemplateEquipmentGetaWayDTO teg = new TemplateEquipmentGetaWayDTO();
                            int rechargeAmount1 = te.getRechargeAmount().intValue();
                            if (totalCount >= rechargeAmount1) {
                                teg.setGiveUP(true);
                                teg.setEquipmentNameNumber(te.getEquipmentNameNumber());
                                teg.setRechargeAmount(rechargeAmount1);
                                teg.setEquipmentStatus(template.getEquipmentStatus());
                                equipmentList.add(teg);
                            }
                        }


                }
            }
            rechargeGetWayDTO.setEquipmentList(equipmentList);
        }

    }

    public static void formatTemplate(TemplateCriteria template, int amount, String payCode, GameRechargeGetWayDTO rechargeGetWayDTO) {

        // 附加赠送
        if (!CollectionUtils.isEmpty(template.getAdditionalList())) {
            List<TemplateAdditionalGetaWayDTO> additionalList = new ArrayList<>();
            template.getAdditionalList().forEach((additional) -> {
                if (additional.getAdditionalStatus().ordinal() > AdditionalStatus.CLOSE.ordinal()) {
                    TemplateAdditionalGetaWayDTO tag = new TemplateAdditionalGetaWayDTO();
                    tag.setAdditionalStatus(additional.getAdditionalStatus());
                    tag.setDonationRatio(additional.getDonationRatio());
                    tag.setIsShow(additional.getIsShow());
                    tag.setMaterialName(additional.getMaterialName());
                    Map map=templateOptionSUM(amount, payCode, additional.getAdditionalStatus(), additional.getDonationRatio(), template);
                    tag.setTotalCount((Integer) map.get("countAmount"));
                    tag.setGiveMoney((String) map.get("giveMoney"));
                    tag.setGiveTotal(String.valueOf(tag.getTotalCount()*tag.getDonationRatio()/10));
                   // tag.setTotalCount(templateOptionSUM(amount, payCode, additional.getAdditionalStatus(), additional.getDonationRatio(), template));
                    additionalList.add(tag);
                }
            });
            rechargeGetWayDTO.setAdditionalList(additionalList);

        }
        // 自定义
        if (template.getCustomizeStatus()!=null && template.getCustomizeStatus()!= AdditionalStatus.CLOSE && !CollectionUtils.isEmpty(template.getCustomizeList())) {
            TemplateCustomizeGetaWayDTO customize = new TemplateCustomizeGetaWayDTO();
            customize.setCustomizeStatus(template.getCustomizeStatus());
            Map map=templateOptionSUM(amount, payCode, template.getCustomizeStatus(), 1, template);
            customize.setTotalCount((Integer) map.get("countAmount"));
            customize.setGiveMoney((String) map.get("giveMoney"));
            rechargeGetWayDTO.setCustomizeGetaWay(customize);
        }
        // 积分
        if (!CollectionUtils.isEmpty(template.getIntegralList())) {
            List<TemplateIntegralGetaWayDTO> integralList = new ArrayList<>();
            template.getIntegralList().forEach((integral) -> {
                if (integral.getIntegralStatus().ordinal() > AdditionalStatus.CLOSE.ordinal()) {
                    TemplateIntegralGetaWayDTO tig = new TemplateIntegralGetaWayDTO();
                    tig.setDonationRatio(integral.getDonationRatio());

                    tig.setIntegralName(integral.getIntegralName());
                    tig.setIntegralStatus(integral.getIntegralStatus());
                    Map map=templateOptionSUM(amount, payCode, integral.getIntegralStatus(), integral.getDonationRatio(), template);
                    tig.setTotalCount((Integer) map.get("countAmount"));
                    tig.setGiveMoney((String) map.get("giveMoney"));
                    tig.setGiveTotal(String.valueOf(tig.getTotalCount()*tig.getDonationRatio()/10));
                   // tig.setTotalCount(templateOptionSUM(amount, payCode, integral.getIntegralStatus(), integral.getDonationRatio(), template));
                    integralList.add(tig);
                }
            });
            rechargeGetWayDTO.setIntegralList(integralList);
        }

        // 装备
        if (template.getEquipmentStatus().ordinal() > AdditionalStatus.CLOSE.ordinal()) {
            List<TemplateEquipmentGetaWayDTO> equipmentList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(template.getEquipmentList())) {
                Map map=templateOptionSUM(amount, payCode, template.getEquipmentStatus(), 100, template);
                int totalCount=(Integer) map.get("countAmount");
                totalCount=totalCount/10;
               // int totalCount = templateOptionSUM(amount, payCode, template.getEquipmentStatus(), 100, template)/10;
                if(template.getShowEquipmentType()==null ||template.getShowEquipmentType()==1) {
                    TemplateEquipmentGetaWayDTO teg = new TemplateEquipmentGetaWayDTO();
                    teg.setEquipmentStatus(AdditionalStatus.CLOSE);
                    int num = 0;
                    for (TemplateEquipmentDTO te : template.getEquipmentList()) {
                        int rechargeAmount=te.getRechargeAmount().intValue();
                        if (totalCount >= rechargeAmount && num < rechargeAmount) {
                            num = rechargeAmount;
                            teg.setGiveUP(true);
                            teg.setEquipmentNameNumber(te.getEquipmentNameNumber());
                            teg.setRechargeAmount(rechargeAmount);
                            teg.setEquipmentStatus(template.getEquipmentStatus());
                        }
                    }
                    equipmentList.add(teg);
                }else {

                        for (TemplateEquipmentDTO te : template.getEquipmentList()) {
                            TemplateEquipmentGetaWayDTO teg = new TemplateEquipmentGetaWayDTO();
                            int rechargeAmount1 = te.getRechargeAmount().intValue();
                            if (totalCount >= rechargeAmount1) {
                                teg.setGiveUP(true);
                                teg.setEquipmentNameNumber(te.getEquipmentNameNumber());
                                teg.setRechargeAmount(rechargeAmount1);
                                teg.setEquipmentStatus(template.getEquipmentStatus());
                                equipmentList.add(teg);
                            }
                        }


                }
            }
            rechargeGetWayDTO.setEquipmentList(equipmentList);
        }

    }

    /**
     * 各选项计算返回 选项金额
     *
     * @param amount
     * @param payCode
     * @param status
     * @param donationRatio
     * @param template
     * @return
     */
    private static Map templateOptionSUM(int amount, String payCode, AdditionalStatus status, int donationRatio, TemplateCriteria template) {
        Map mapMoney=new HashMap();
        int amounts = amount*10;
        int countAmount=amounts;
        int giveMoney=0;
        switch (status) {
            case AMOUNT:

                break;
            case AMOUNT_CHANNEL:
                if (template.getIsRepaidGift() == YesAndNo.YES) {
                    Map<String, Integer> map = getChannelMap(template.getChannelList());
                    Integer donationRatioChannel = map.get(payCode);
                    if(null == donationRatioChannel){
                        donationRatioChannel=0;
                    }
                    giveMoney=amounts * donationRatioChannel / 100;
                    countAmount = (amounts + giveMoney) ;
                }
                break;
            case AMOUNT_ESCITATION:
                //template.getIsShowAmount() == YesAndNo.YES &&
                if (!CollectionUtils.isEmpty(template.getEscitationList())) {
                    int escitation1 = getEscitationMap(template.getEscitationList(), amount);
                    giveMoney=escitation1*10;
                    countAmount = (amounts + giveMoney);
                }
                break;
            case AMOUNT_CHANNEL_ESCITATION:
                int escitation =0;
                if (!CollectionUtils.isEmpty(template.getEscitationList())) {
                    escitation =  getEscitationMap(template.getEscitationList(), amount);
                    giveMoney=giveMoney+escitation*10;
                }
                if (template.getIsRepaidGift() == YesAndNo.YES) {
                    Map<String, Integer> channelMap = getChannelMap(template.getChannelList());
                    Integer donationRatioChannel2=channelMap.get(payCode);
                    if(null == donationRatioChannel2){
                        donationRatioChannel2=0;
                    }
                    int channelAmount=amounts;
                    if (template.getIsShowAmount() == YesAndNo.YES) {
                        channelAmount=channelAmount+escitation*10;
                    }
                    int channelOfMoney = channelAmount * donationRatioChannel2 / 100;
                    giveMoney=giveMoney+channelOfMoney;

                }
                countAmount=countAmount+giveMoney;
                break;
        }
        mapMoney.put("countAmount",countAmount);
        mapMoney.put("giveMoney",formatMoney(giveMoney));
        return mapMoney;
       // return countAmount;
    }

    /**
     * 各选项计算返回 选项金额
     *
     * @param amount
     * @param payCode
     * @param status
     * @param donationRatio
     * @param template
     * @return
     */
    private static Map templateOptionSUM_1(int recAmount, int amount , String payCode, AdditionalStatus status, int donationRatio, TemplateCriteria template, boolean typeTwo) {
        Map mapMoney=new HashMap();
        int amounts = amount*10;
        int countAmount=amounts;
        int giveMoney=0;
        switch (status) {
            case AMOUNT:

                break;
            case AMOUNT_CHANNEL:
                if (template.getIsRepaidGift() == YesAndNo.YES) {
                    Map<String, Integer> map = getChannelMap(template.getChannelList());
                    Integer donationRatioChannel = map.get(payCode);
                    if(null == donationRatioChannel){
                        donationRatioChannel=0;
                    }
                    giveMoney=amounts * donationRatioChannel / 100;
                    countAmount = (amounts + giveMoney) ;
                }
                break;
            case AMOUNT_ESCITATION:
                //template.getIsShowAmount() == YesAndNo.YES &&
                if (!CollectionUtils.isEmpty(template.getEscitationList())) {
                    int escitation1 = getEscitationMap(template.getEscitationList(), amount);
                    giveMoney=escitation1*10;
                    countAmount = (amounts + giveMoney);
                }
                break;
            case AMOUNT_CHANNEL_ESCITATION:

                int escitation =0;
                if (!CollectionUtils.isEmpty(template.getEscitationList())) {
                    escitation =  getEscitationMap(template.getEscitationList(), amount);
                    giveMoney=giveMoney+escitation*10;
                }
                if (template.getIsRepaidGift() == YesAndNo.YES) {
                    Map<String, Integer> channelMap = getChannelMap(template.getChannelList());
                    Integer donationRatioChannel2=channelMap.get(payCode);
                    if(null == donationRatioChannel2){
                        donationRatioChannel2=0;
                    }
                    int channelAmount=amounts;
                    if (template.getIsShowAmount() == YesAndNo.YES) {
                        channelAmount=channelAmount+escitation*10;
                    }
                    int channelOfMoney = channelAmount * donationRatioChannel2 / 100;
                    giveMoney=giveMoney+channelOfMoney;

                }
                countAmount=countAmount+giveMoney;
                break;
        }
        if(typeTwo){
            mapMoney.put("countAmount",countAmount +amounts*donationRatio/100);
            mapMoney.put("giveMoney",formatMoney(giveMoney+countAmount*donationRatio/100));
            return mapMoney;
        }else {
            mapMoney.put("countAmount",countAmount +countAmount*donationRatio/100);
            mapMoney.put("giveMoney",formatMoney(giveMoney+countAmount*donationRatio/100));
            return mapMoney;
        }
    }

    public static String formatMoney(int money){
        if(0==money){
            return "0.0";
        }
        double m=money;
        double f = m/10;
        DecimalFormat df = new DecimalFormat("#.0");
        return df.format(f);
    }


    private static TemplateIntegralGetaWayDTO integralFmt() {

        return null;
    }

//    //附加赠送
//  private static TemplateAdditional additionalFmt(int amount, PayType payTyp, TemplateAdditional additional, TemplateCriteria template){
//      switch(additional.getAdditionalStatus()) {
//          case AMOUNT:
//              additional.setTotalCount(amount*additional.getDonationRatio());
//              break;
//          case  AMOUNT_CHANNEL:
//              if(template.getIsRepaidGift()== YesAndNo.YES) {
//                  Map<String, Integer> map = getChannelMap(template.getChannelList());
//                  int donationRatio = map.get(payTyp.getDesc());
//                  additional.setTotalCount((amount+ donationRatio)* additional.getDonationRatio()/100);
//              }
//              break;
//          case  AMOUNT_ESCITATION:
//              if(template.getIsShowAmount()== YesAndNo.YES && !CollectionUtils.isEmpty(template.getEscitationList())) {
//
//                  int escitation = getEscitationMap(template.getEscitationList(),amount);
//                  additional.setTotalCount((amount + escitation) * additional.getDonationRatio()/100);
//              }
//              break;
//          case  AMOUNT_CHANNEL_ESCITATION:
//              Map<String,Integer> channelmap=getChannelMap(template.getChannelList());
//              int donationRatios=channelmap.get(payTyp.getDesc());
//              int escitations=getEscitationMap(template.getEscitationList(),amount);
//              additional.setTotalCount((amount+amount*donationRatios/100+escitations)*additional.getDonationRatio()/100);
//              break ;
//      }
//      return additional;
//  }


    private static Map getChannelMap(List<TemplateChannelDTO> channelList) {
        Map<String, Integer> map = new HashMap();
        if (!CollectionUtils.isEmpty(channelList)) {
            channelList.forEach((channels) -> {
                map.put(channels.getCode(), channels.getDonationRatio());
            });
        }
        return map;
    }

    private static Map getChannelMap_1(List<TemplateChannelDTO> channelList) {
        Map<String, TemplateChannelDTO> map = new HashMap();
        if (!CollectionUtils.isEmpty(channelList)) {
            channelList.forEach((channels) -> {
                map.put(channels.getCode(), channels);//channels.getDonationRatio()
            });
        }
        return map;
    }

    //返回激历金
    private static int getEscitationMap(List<TemplateEscitationDTO> escitationList, int amount) {

        int eAmount = 0;
        int num=0;
        for (TemplateEscitationDTO escitation : escitationList) {
            int esAmount = escitation.getRechargeAmount().intValue();

            if (amount >= esAmount && num<esAmount) {
                num=escitation.getRechargeAmount().intValue();
                eAmount = escitation.getEscitationAmount().intValue();
            }

        }
        return eAmount;
    }

    //返回激历金
    private static int getEscitationMap_1(List<TemplateEscitationDTO> escitationList, int amount,GameRechargeGetWayDTO rechargeGetWayDTO) {

        int eAmount = 0;
        int num=0;
        for (TemplateEscitationDTO escitation : escitationList) {
            int esAmount = escitation.getRechargeAmount().intValue();

            if (amount >= esAmount && num<esAmount) {
                num=escitation.getRechargeAmount().intValue();
                eAmount = escitation.getEscitationAmount().intValue();
                rechargeGetWayDTO.setEscitationDTO(escitation);
            }

        }
        return eAmount;
    }


    public static void main(String[] args) {
       System.out.println(TemplateFormatUtil.formatMoney(0));

    }

}
